<?php

namespace app\api\controller;

use \tpfcore\Core;

/**
 * @title   小程序商城接口类
 * @description
 * @group   小程序接口
 * Class Goods
 * @package app\api\controller
 */
class Goods extends ApiBase
{
    /**
     * @title       获取商品列表
     * @description 商品列表
     * @author      TPFrame
     * @url         /api/Goods/vlist/
     * @method POST
     * @param_send  name:api_version type:string require:1 default:"1.0.0" other:格式x.x.x desc:版本号
     * @param_send  name:limit type:integer require:0 default:10 other:xxx desc:每页数据条数
     */
    public function vlist()
    {
        $data = $this->param;
        $where = ["is_on_sale"=>1,"status"=>1,"is_delete"=>0];
        $order = "add_time desc";
        if(!empty($data['sort'])){
            switch ($data['sort']) {
                case 'update':
                    $order="last_update_time desc";
                    break;
                case 'comment':
                    $order="comment_sum desc";
                    break;
                case 'sales_sum':
                    $order="sales desc";
                    break;
                case 'price_asc':
                    $order="price asc";
                    break;
                case 'price_desc':
                    $order="price desc";
                    break;
                default:
                    # code...
                    break;
            }
        }

        $limit = isset($data['limit']) && !empty($data['limit']) ? (int)$data['limit'] : 10;
        switch ($this->api_version) {
            case '1.0':
                $this->jump(Core::loadModel($this->name)->getGodos([
                    "field" => 'id,goods_name,goods_thumb,price,market_price,sales_sum,virtual_sales,sales_sum+virtual_sales as sales',
                    'where' => $where,
                    'order' => $order,
                    'paginate' => ['rows' => $limit, 'config' => ['query' => request()->param()]]
                ]));
                break;
            default:
                $this->jump([40048, '版本号错误']);
                break;
        }
    }
    /**
     * @title       获取商品详情
     * @description 商品详情
     * @author      TPFrame
     * @url         /api/Goods/detail/
     * @method POST
     * @param_send  name:api_version type:string require:1 default:"1.0.0" other:格式x.x.x desc:版本号
     * @param_send  name:id type:integer require:1 default:"" other:xxx desc:产品id
     */
    public function detail(){
        switch ($this->api_version) {
            case '1.0':
                $this->jump(Core::loadModel($this->name)->detail($this->param));
                break;
            default:
                $this->jump([40048, '版本号错误']);
                break;
        }
    }
    /**
     * @title       获取商品评论
     * @description 商品评论
     * @author      TPFrame
     * @url         /api/Goods/comment/
     * @method POST
     * @param_send  name:api_version type:string require:1 default:"1.0.0" other:格式x.x.x desc:版本号
     * @param_send  name:id type:integer require:1 default:"" other:xxx desc:产品id
     */
    public function comment(){
        switch ($this->api_version) {
            case '1.0':
                $this->jump(Core::loadModel($this->name)->comment($this->param));
                break;
            default:
                $this->jump([40048, '版本号错误']);
                break;
        }
    }
     /**
     * @title       添加商品到购物车
     * @description 添加购物车
     * @author      TPFrame
     * @url         /api/Goods/addCart/
     * @method POST
     * @param_send  name:api_version type:string require:1 default:"1.0.0" other:格式x.x.x desc:版本号
     * @param_send  name:id type:integer require:1 default:"" other:xxx desc:产品id
     * @param_send  name:quantity type:integer require:1 default:"" other:xxx desc:产品购买数量
     * @param_send  name:spec_key type:string require:0 default:"" other:xxx desc:商品规格key,逗号分割
     * @param_send  name:spec_key_name type:string require:0 default:"" other:xxx desc:规格对应的中文名字
     */
    public function addCart(){
        switch ($this->api_version) {
            case '1.0':
                $this->jump(Core::loadModel($this->name)->addCart($this->param));
                break;
            default:
                $this->jump([40048, '版本号错误']);
                break;
        }
    }
    // 添加评论
    public function addComment(){
        switch ($this->api_version) {
            case '1.0':
                $this->jump(Core::loadModel($this->name)->addComment($this->param));
                break;
            default:
                $this->jump([40048, '版本号错误']);
                break;
        }
    }
}